$(function () {
    var role_datagrid = $("#role_datagrid");
    var role_dialog = $("#role_dialog");
    var role_form = $("#role_form");
    var allPermissions = $("#allPermissions");
    var selectedPermissions = $("#selectedPermissions");
    var menu_combotree = $("#menu_combotree");
    role_datagrid.datagrid({
        url: '/role/query.do',
        fit: true,
        fitColumns: true,
        columns: [[
            {field: 'sn', title: '角色编号', width: 100,},
            {field: 'name', title: '角色名称', width: 100,}
        ]],
        singleSelect: true,
        pagination: true,
        toolbar: "#role_btns",
    });
    role_dialog.dialog({
        width: 550,
        height: 500,
        buttons: "#save_btns",
        closed: true,
        onClose: function () {
            role_form.form('clear');
            selectedPermissions.datagrid('loadData', []);
            allPermissions.datagrid('load');
        }
    });
    var methodObj = {
        add: function () {
            role_dialog.dialog('setTitle', '新增角色');
            role_dialog.dialog('open');
        },

        edit: function () {
            var row = role_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            //回显权限数据
            selectedPermissions.datagrid('load', {roleId: row.id});
            //回显菜单数据
            $.get('/role/getMenuByRoleId.do', {roleId: row.id}, function (data) {
                menu_combotree.combotree('setValues', data);
            })
            role_form.form('load', row
            );
            role_dialog.dialog('setTitle', '编辑角色');
            role_dialog.dialog('open');
        },
        changeState: function () {
            var row = role_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定执行该操作吗？', function (r) {
                if (r) {
                    $.get("/role/changeState.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功', 'info')
                            role_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        del: function () {
            var row = role_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定要删除该角色吗？', function (r) {
                if (r) {
                    $.get("/role/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '删除成功', 'info')
                            role_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        reload: function () {
            role_datagrid.datagrid('reload');
        },

        save: function () {
            role_form.form('submit', {
                url: '/role/saveOrUpdate.do',
                onSubmit: function (param) {
                    //拿到已选中权限中全部的内容
                    var rows = selectedPermissions.datagrid('getRows');
                    for (var i = 0; i < rows.length; i++) {
                        param['permissions[' + i + '].id'] = rows[i].id;
                    }
                    //拿到已选中菜单的内容
                    var menus = menu_combotree.combotree('getValues');
                    for (var i = 0; i < rows.length; i++) {
                        param['menus[' + i + '].id'] = menus[i];
                    }
                },
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info')
                        methodObj.cancel();
                        role_datagrid.datagrid('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },

        cancel: function () {
            role_dialog.dialog('close');
        }
    }
    $("a[data-cmd]").click(function () {
        var methodName = $(this).data('cmd');
        methodObj[methodName]();
    })
    allPermissions.datagrid({
        title: '所有权限',
        url: '/permission/selectAll.do',
        fitColumns: true,
        height: 320,
        columns: [[
            {field: 'name', title: '权限名称', width: 100, align: 'center'},
        ]],
        rownumbers: true,
        onClickRow: function (index, row) {
            selectedPermissions.datagrid("appendRow", row);
            allPermissions.datagrid('deleteRow', index);
        }
    })
    selectedPermissions.datagrid({
        title: '已有权限',
        url: '/permission/getPermissionByRoleId.do',
        fitColumns: true,
        height: 320,
        columns: [[
            {field: 'name', title: '权限名称', width: 100, align: 'center'},
        ]],
        rownumbers: true,
        onClickRow: function (index, row) {
            allPermissions.datagrid("appendRow", row);
            selectedPermissions.datagrid('deleteRow', index);
        },
        onLoadSuccess: function (data) {
            //在加载成功之后将左右两边重复的数据从所有权限当中删除
            var ids = $.map(data.rows, function (row) {
                return row.id;
            });
            var rows = allPermissions.datagrid('getRows');
            for (var i = rows.length - 1; i >= 0; i--) {
                var index = $.inArray(rows[i].id, ids);
                if (index >= 0) {
                    allPermissions.datagrid('deleteRow', i);
                }
            }
        }
    });
    menu_combotree.combotree({
        formatter: function (node) {
            return node.name;
        },
        url: '/menu/getRootMenu.do',
        multiple: true,
        cascadeCheck: false,
        textField: 'name',
        onCheck: function (node, checked) {
            var tree = menu_combotree.combotree('tree');
            var parent = tree.tree('getParent', node.target);
            if (parent) {
                tree.tree('check', parent.target);
            }
        }
    })
})



